package com.zs.grain.service;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import  com.zs.grain.service.SoundService;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.zs.grain.R;

import java.io.IOException;

import static com.jeremyliao.liveeventbus.utils.AppUtils.getApp;

public class MediaPlayerService {
    MediaPlayer mp1 = null;
    private AudioManager mAudioManager;

    public void initPlayer(){
        if (mp1 == null) {
            mAudioManager = (AudioManager) getApp().getSystemService(Context.AUDIO_SERVICE);
            mp1 = new MediaPlayer();
        }
    }

    public void playMusic2(int index) {
        if (mp1 == null) {
            mAudioManager = (AudioManager) getApp().getSystemService(Context.AUDIO_SERVICE);
            mp1 = new MediaPlayer();
            //Log.d(TAG, "max notify vol is" + mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) - 14, AudioManager.FLAG_PLAY_SOUND);
            mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION)/5, AudioManager.FLAG_PLAY_SOUND);
        }else{
            mp1.release();
            mAudioManager = (AudioManager) getApp().getSystemService(Context.AUDIO_SERVICE);
            //mp1 = new MediaPlayer();
            //Log.d(TAG, "max notify vol is" + mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) - 14, AudioManager.FLAG_PLAY_SOUND);
            mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION)/5, AudioManager.FLAG_PLAY_SOUND);
        }
        mp1.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

        try {
            switch (index){
                case 1:mp1.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.dingdong));
                    break;
                case 2:mp1.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.dosage));
                    break;
                case 3:mp1.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.scan));
                    break;
                case 4:mp1.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.making));
                    break;
                case 5:mp1.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.take_2));
                    break;
                case 6:mp1.setDataSource(getApp(), Uri.parse("android.resource://com.zs.grain/" + R.raw.finish));
                    break;
                default:break;
            }
            //mp1.setDataSource(getApplicationContext(), Uri.parse("android.resource://com.zs.grain/" + R.raw.making));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mp1.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) - 8, AudioManager.FLAG_PLAY_SOUND);
                mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 1, AudioManager.FLAG_PLAY_SOUND);
            }
        });
        mp1.start();
    }

    public void release(){
        if (mp1 != null) {
            mp1.release();
            //mAudioManager.
        }
    }

    public void stopPlayer(){
        if (mp1 != null) {
            mp1.stop();
        }
    }
}
